import js from '@eslint/js'
import pluginImport from 'eslint-plugin-import'
import prettier from 'eslint-plugin-prettier'
import prettierRecommended from 'eslint-plugin-prettier/recommended'
import react from 'eslint-plugin-react'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'
import globals from 'globals'

/** @type {import('eslint').Linter.Config[]} */
export default [
  { ignores: ['dist', 'public'] },
  {
    files: ['**/*.{js,jsx}'],
    languageOptions: {
      ecmaVersion: 2020,
      globals: {
        ...globals.browser,
        ...globals.node,
        ...globals.es2018,
      },
      parserOptions: {
        ecmaVersion: 'latest',
        ecmaFeatures: { jsx: true },
        sourceType: 'module',
      },
    },
    settings: { react: { version: '18.3' } },
    plugins: {
      react,
      'react-hooks': reactHooks,
      'react-refresh': reactRefresh,
      import: pluginImport,
      prettier,
    },
    rules: {
      ...prettierRecommended.rules,
      ...js.configs.recommended.rules,
      ...react.configs.recommended.rules,
      ...react.configs['jsx-runtime'].rules,
      ...reactHooks.configs.recommended.rules,
      'react/jsx-no-target-blank': 'off',
      'react-refresh/only-export-components': ['warn', { allowConstantExport: true }],
      'no-useless-escape': 0,
      'no-unused-vars': 1,
      'react/prop-types': 0,
      'react-hooks/exhaustive-deps': 0,
      'react/display-name': 0,
      eqeqeq: ['error', 'always'],
      camelcase: 2, // 驼峰命名
      // 配置 import 排序规则
      'import/order': [
        'error',
        {
          groups: [
            'external', // 第三方依赖（如 lodash、react）
            'builtin', // Node.js 内置模块
            'internal', // 项目内部模块（别名路径或相对路径）
            'object', // 对象导入（如 TypeScript 类型）
            'type', // TypeScript 类型（需配合其他配置）
            'parent', // 父级目录模块
            'sibling', //兄弟目录模块
            'index', // 当前目录的 index 文件
          ],
          alphabetize: {
            order: 'asc', // 按字母升序排列
            caseInsensitive: true, // 忽略大小写
          },
          'newlines-between': 'always', // 分组间空行分隔
          pathGroups: [
            {
              pattern: '@/**', // 示例：将 `@/` 别名视为内部模块
              group: 'internal',
              position: 'after', // 在 internal 分组内排序
            },
          ],
        },
      ],
    },
  },
]
